using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.Registry;
using Deepcode.Flux.Core.UI.Controls;
using Deepcode.Flux.Core.Systems.UI.Menus;

public partial class Menu_appmenu : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			AddinTreeNode root = AddinTree.Tree.GetTreeNodeNoThrow("/Flux/Menus/MainMenu/");
			if (root != null)
			{
				menugrouplist.DataSource = root.BuildChildItems(this);
				menugrouplist.DataBind();
			}
		}
	}

	private FluxRegistry __AppReg = null;
	private FluxRegistry ApplicationRegistry
	{
		get
		{
			if (__AppReg == null)
				__AppReg = new FluxRegistry(FluxRegistryRoot.CURRENT_USER, "/MainMenu/Expanded");

			return __AppReg;
		}
	}

	protected void GroupItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (!
			((e.Item.ItemType == ListItemType.AlternatingItem) ||
			 (e.Item.ItemType == ListItemType.EditItem) ||
			 (e.Item.ItemType == ListItemType.Item) ||
			 (e.Item.ItemType == ListItemType.SelectedItem))) return;

		// Get the section control
		CollapseSection CSection = (CollapseSection) e.Item.FindControl("csection");

		// Get the group being bound
		MenuGroup mg = (MenuGroup)e.Item.DataItem;

		// Set expand / collapse of section based on the user's preference
		CSection.Expanded = ApplicationRegistry.GetKey(mg.ID, false);
		CSection.Tag = mg.ID;
		// Get the sub repeater
		Repeater menuoptionslist = (Repeater)e.Item.FindControl("menuoptionslist");
		if (menuoptionslist == null) return;
		// Bind the options
		menuoptionslist.DataSource = mg.Options;
		menuoptionslist.DataBind();
	}

	private int LastID = 1;
	
	protected int NextID()
	{
		return LastID;
	}
	protected int IncID()
	{
		return LastID++;
	}

	private void MenuSectionExpandCollapse(Deepcode.Flux.Core.UI.Controls.CollapseSection source)
	{
		if (source.Expanded) ApplicationRegistry.SetKey(source.Tag.ToString(), true);
		else ApplicationRegistry.DeleteKey(source.Tag.ToString());
	}

	protected void GroupItemCommand(object source, RepeaterCommandEventArgs e)
	{
		if (e.CommandName == "ExpandCollapse" && e.CommandArgument is Deepcode.Flux.Core.UI.Controls.CollapseSection)
			MenuSectionExpandCollapse((Deepcode.Flux.Core.UI.Controls.CollapseSection)e.CommandArgument);
	}


}
